package com.xjgc.sap.ws.dao;

import com.xjgc.common.config.DbConn;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class ZxjEip2Cvbelnops {
	public String update(DbConn db){
		
		String sql = "select no,shipmentstatus,deliverystatus from zxj_eip_2cvbeln where shipmentstatus <>'8' ";
		String updateSql = "update zxj_eip_2cvbeln set shipmentstatus = ?,ioflag='1',mstatus='2' where no=?";
		
		PreparedStatement pst = null;
		PreparedStatement updatePst = null;
		ResultSet rs = null;
		String result = null;
		
		try {
			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();
			updatePst = db.getConnetion().prepareStatement(updateSql);
			while(rs.next()) {
				String oldShipmentstatus = rs.getString("shipmentstatus");
				String newShipmentstatus = getNewStatus(rs.getString("no"),rs.getString("shipmentstatus"),rs.getString("deliverystatus"),db);
//				System.out.println("************"+rs.getString("no")+"********   oldShipmentstatus="+oldShipmentstatus+"           newShipmentstatus="+newShipmentstatus);
				
				if(oldShipmentstatus==newShipmentstatus||oldShipmentstatus.equals(newShipmentstatus)) {
				}else {//状态有变化需要更新
					updatePst.setString(1,newShipmentstatus);
					updatePst.setString(2, rs.getString("no"));
					updatePst.execute();
				}
			}
			updatePst.close();
			rs.close();
			pst.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.closeCn();
		}

		return result ;
	}
	
	//根据销售订单号和当前shipmentstatus值进行判断更新
	public String getNewStatus(String no,String shipmentstatus,String deliverystatus,DbConn db) {
		
		String nStatus = shipmentstatus;
		
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		String sql35 = "select actualstarttime,productionstatus from zxj_eip_2caufnr where salesorderno ='"+no+"'";
		String sql6 = "select count(*) \"counts\" from zxj_mes_vbelv where \"VGBEL\" = '"+no+"'";
		try {		
			if(Integer.parseInt(shipmentstatus)<3) {//3-生产中  
				pst = db.getConnetion().prepareStatement(sql35);
				rs = pst.executeQuery();
				while(rs.next()) {
//					System.out.println("*************333333333333333****************======= "+rs.getString("actualstarttime"));
					if (rs.getString("actualstarttime") == "1900-01-01 00:00:00"||rs.getString("actualstarttime").equals("1900-01-01 00:00:00")) {
					}else {
						return nStatus="3";
					}
				}
				pst.close();
				db.closeCn();
			}else if(Integer.parseInt(shipmentstatus)<5) {//5-待发货
				
				pst = db.getConnetion().prepareStatement(sql35);
				rs = pst.executeQuery();
				boolean bl = true;
				while(rs.next()) {
//					System.out.println("***************555555555555555555******************===="+rs.getString("productionstatus"));
					if (rs.getString("productionstatus") == "4"||rs.getString("productionstatus").equals("4")) {
					}else {
						bl = false;
					}
				}
				if(bl) {
					return nStatus="5";
				}
				pst.close();
				db.closeCn();
			}else if(Integer.parseInt(shipmentstatus)<6) { //6-部分发货
				pst = db.getConnetion().prepareStatement(sql6);
				rs = pst.executeQuery();
				while(rs.next()) {
//					System.out.println("***************6666666666666666******************===="+rs.getInt("counts"));
					if (rs.getInt("counts") > 0) {
						return nStatus="6";
					}
				}
				pst.close();
				db.closeCn();
			}else if(deliverystatus.equals("2")&&Integer.parseInt(shipmentstatus)<7) {//7-发货完成
//				System.out.println("***************777777777777777******************===="+deliverystatus);
				return nStatus="7";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.closeCn();
		}
		return nStatus;
	}

}
